/**  
* @Title: CustomerCardsController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年5月20日  
*/
package com.fortune.rainshine.system.controller;

import java.util.List;
import java.util.Set;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.system.service.CustomerCardService;
import com.fortune.rainshine.system.service.CustomerService;
import com.fortune.rainshine.system.service.MembershipCardService;
import com.fortune.rainshine.system.service.RedisService;
import com.fortune.rainshine.system.service.SmsSendFeign;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.RedisConstantEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.CustomerModel;
import com.fortune.rainshinecommon.model.SmsParamsModel;
import com.fortune.rainshinecommon.model.system.CardModel;
import com.fortune.rainshinecommon.model.system.CustomerCardModel;
import com.fortune.rainshinecommon.util.RandomNumUtil;
import com.google.gson.Gson;

/**
 * @ClassName: CustomerCardsController
 * @Description: TODO(顾客会员卡功能)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年5月20日
 * 
 */
@Controller
@RequestMapping("/system/customerCard")
public class CustomerCardController extends BaseController {
	private final static Logger logger = LoggerFactory.getLogger(CustomerCardController.class);

	@Autowired
	private CustomerCardService service;
	@Autowired
	private MembershipCardService cardService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private RedisService redisFeign;
	@Autowired
	private SmsSendFeign smsSendFeign;
	@Value("${smsSend.number}")
	private Integer number;

	@RequiresPermissions("customerCard:view")
	@RequestMapping()
	public String card(Model model) {
		return "customerCard/customerCardList.html";
	}

	@RequestMapping("/editForm")
	public String addUser(Integer shopId, Model model) {
		List<CardModel> cards = cardService.getAllCards();
		ParmsModel parm = new ParmsModel(0, 999, null, null, shopId);
		List<CustomerModel> customers = (List<CustomerModel>) customerService.listActive(parm).getData();
		model.addAttribute("cards", cards);
		model.addAttribute("customers", customers);

		
		return "customerCard/customerCard_form.html";
	}

	/**
	 * @Title: add
	 * @Description: 顾客购买会员卡
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月15日
	 */
	@RequiresPermissions("customerCard:add")
	@ResponseBody
	@RequestMapping("/add")
	public MsgModel add(@RequestBody CustomerCardModel model) {
		MsgModel msg = new MsgModel();
		try {
			// 判断用户是否存在
			CustomerModel customer = (CustomerModel) customerService.getCustomerById(model.getCustomerId());
			// redis key
			String key = RedisConstantEnum.BUYCARD.getMessage() + customer.getCustomerPhone();
			if (customer != null) {
				// 判断是否存在于redis中
				Boolean exists = redisFeign.exists(key);
				if (exists) {
					// 获取redis中的key
					String sms = (String) redisFeign.get(key).getData();
					if (sms.equals(model.getVerificationCode())) {
						model.setUser(this.getLoginUser());
						model.setCustomerCode(customer.getCustomerCode());
						msg = service.add(model);
						redisFeign.delete(key);
					} else {
						msg = new MsgModel(ResultCode.FAILURE, "验证码不正确");
					}
				} else {
					msg = new MsgModel(ResultCode.FAILURE, "验证码不存在或者已过期");
				}
			} else {
				msg = new MsgModel(ResultCode.FAILURE, "用户不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
		return msg;
	}

	/**
	 * @Title: list
	 * @Description: 列表
	 * @author zhangxin
	 * @date 2019年3月26日
	 */
	@RequiresPermissions("customerCard:view")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public MsgModel list(Integer page, Integer limit, String searchKey, String searchValue, Integer shopId) {
		try {
			if (page == null) {
				page = 0;
				limit = 0;
			}
			if (StringUtil.isBlank(searchValue)) {
				searchKey = null;
			}

			ParmsModel parm = new ParmsModel(page, limit, searchKey, searchValue, shopId);
			MsgModel ms = service.list(parm);
			return ms;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}

	}

	/**
	 * @Title: sms
	 * @Description: 购买会员卡时发送验证码
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月21日
	 */
	@RequiresPermissions("customerCard:sms")
	@ResponseBody
	@RequestMapping("/sms")
	public MsgModel sms(@RequestBody CustomerCardModel customerCard) {
		try {
			CustomerModel customer = (CustomerModel) customerService.getCustomerById(customerCard.getCustomerId());
			String phone = customer.getCustomerPhone();
			Boolean exists = redisFeign.exists(RedisConstantEnum.BUYCARD.getMessage() + phone);
			Boolean bo = false;
			if (exists) {
				redisFeign.delete(RedisConstantEnum.BUYCARD.getMessage() + phone);
			}
			Gson gson = new Gson();
			String code = RandomNumUtil.getRandNum(number);
			SmsParamsModel smsParams = new SmsParamsModel();
			smsParams.setCode(code);
			String jsonParams = gson.toJson(smsParams);

			try {
				bo = smsSendFeign.sendSms(phone, jsonParams);
				if (bo) {
					redisFeign.set(RedisConstantEnum.BUYCARD.getMessage() + phone, code, 60000);
				}
				return new MsgModel(ResultCode.SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				return new MsgModel(ResultCode.FAILURE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
	}

}
